package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.domain.*;
import com.service.IPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-07
 */
@RestController
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private IPatientService iPatientService;

    @GetMapping
    public Result getAll(){
        List<Patient> list = iPatientService.list();
        if(list!=null){
            return new Result(list,"查询成功", Code.GET_OK);
        }else
        {
            return new Result("查询失败", Code.GET_ERR);
        }

    }
//模糊请求
    @GetMapping("/{patientName}")
    public  Result getByName2(@PathVariable String patientName){
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("patient_name",patientName);
        List<Patient> list = iPatientService.list(queryWrapper);
        if(list.isEmpty()){
            return new Result("查询失败", Code.GET_ERR);
        }else
        {
            return new Result(list,"查询成功", Code.GET_OK);
        }
    }

    @PostMapping
    public Result add(@RequestBody Patient patient){
        boolean flag = iPatientService.save(patient);
        if(flag){
            return new Result("添加成功", Code.SAVE_OK);
        }else
        {
            return new Result("添加失败", Code.SAVE_ERR);
        }
    }

    @DeleteMapping("/{patientId}")
    public Result deleteById(@PathVariable Integer patientId){
        boolean flag = iPatientService.removeById(patientId);
        if(flag){
            return new Result("删除成功", Code.DELETE_OK);
        }else
        {
            return new Result("删除失败", Code.DELETE_ERR);
        }
    }

    @PutMapping
    public Result update(@RequestBody Patient patient){
        boolean flag = iPatientService.updateById(patient);
        if(flag){
            return new Result("修改成功", Code.UPDATE_OK);
        }else
        {
            return new Result("修改失败", Code.UPDATE_ERR);
        }
    }

    @PostMapping("/s")
    public Result getByName(String name){
        List<Patient> list = iPatientService.getByName(name);
        if(list!=null){
            return new Result(list,"查询成功", Code.GET_OK);
        }else
        {
            return new Result("查询失败", Code.GET_ERR);
        }
    }
}

